import request from '@/utils/request';
import { Promise } from 'jszip/lib/external';
// 获取角色列表
export function roleList (params) {
  return request ({
    url: '/sys/role',
    method: 'get',
    params: {
      ...params,
    }
  })
    .then(res => ({
      rows: res.data.data.rows.map((d, i) => ({
        ...d,
        index: (params.page - 1) * params.size + i + 1,
      })),
      total: res.data.data.total
    }))
    .catch(err => err)
}
// 获取角色详情
export function roleDetails (id) {
  return request ({
    url: '/sys/user/' + id,
    method: 'get',
  })
  .then(res => {
    // console.log(res.data)
    return res.data.data
  })
  .catch(err => err)
}
// 删除员工
export function deleteRole (id) {
  return request ({
    url: '/sys/user/' + id,
    method: 'delete',
  })
  .then(res => {
    // console.log(res.data)
    return res.data.message
  })
  .catch(err => err)
}
// 增加角色
export function AddRole (data) {
  return request ({
    url: '/sys/role',
    method: 'post',
    data: {
      ...data,
    }
  })
  .then(res => {
    // console.log(res.data)
    return res.data.message
  })
  .catch(err => Promise.reject(err.message))
}
// 删除角色
export function removeRole (id) {
  return request ({
    url: '/sys/role/' + id,
    method: 'delete'
  })
  .then(res => {
    // console.log(res.data)
    return res.data.message
  })
  .catch(err => Promise.reject(err.response.data.message))
}
// 获取角色详情信息
export function getRole (id) {
  return request ({
    url: '/sys/role/' + id,
    method: 'get'
  })
  .then(res => {
    // console.log(res.data.data)
    return res.data.data
  })
  .catch(err => err)
}
// 更新角色信息
export function updateRole (data) {
  return request ({
    url: '/sys/role/' + data.id,
    method: 'put',
    data: {
      ...data
    }
  })
  .then(res => {
    // console.log(res.data)
    return res.data.message
  })
  .catch(err => Promise.reject(err.response.data.messagerr))
}
//获取权限列表
export function getJurisdiction () {
  return request ({
    url: '/sys/permission',
    method: 'get',
  })
  .then( res => res.data.data)
  .catch(err => err)
}

// 给角色分配权限
export function  permissionAssignment (data) {
  return request ({
    url: '/sys/role/assignPrem',
    method: 'put',
    data: {
      ...data
    }
  })
  .then( res => res.data.message)
  .catch(err => Promise.reject(err.response.data.message))
}
